package cn.zhibang.sannuo.user_detection.api;

import cn.zhibang.sannuo.commons.model.*;
import cn.zhibang.sannuo.user_detection.service.UCheckUpService;
import cn.zhibang.sannuo.user_detection.service.UCudetailsService;
import cn.zhibang.sannuo.user_detection.service.UCustomerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
public class UserApi {


    @Autowired private UCustomerService uCustomerService;
    @Autowired private UCheckUpService uCheckUpService;
    @Autowired private UCudetailsService uCudetailsService;
    @Autowired CommonResult commonResult;

    /**
     * 查询所有检测者
     * @return
     */
    @PostMapping("/getAllUser")
    public CommonResult getAllUser(String name, String phone, String bname){
        if (name == "")name=null;
        if (phone == "")phone=null;
        if (bname == "" || "全部".equals(bname))bname=null;
        List<Customer> customer = uCustomerService.getCustomer(name, phone, bname);
        commonResult.setData(customer);
        return commonResult;
    }

    /**
     * 添加检测者
     * @param customer
     * @return
     */
    @PostMapping("/addUser")
    public CommonResult addUser(Customer customer, Model model){
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        String trim = customer.getCname().trim();
        customer.setCname(trim);

        BranchOffice branchOffice = new BranchOffice();
        branchOffice.setBid(2);

        customer.setBranchOffice(branchOffice);
        uCustomerService.addCustomer(customer);
        session.setAttribute("custid",customer);
        return commonResult;
    }
    /**
     * 获取所有检测项目
     * @return
     */
    @PostMapping("/getAllCheckUp")
    public CommonResult getAllCheckUp(){
        List<CheckUp> allCheckUp = uCheckUpService.getAllCheckUp();
        commonResult.setData(allCheckUp);
        return commonResult;
    }

    @PostMapping("/percudetails")
    public CommonResult percudetails(String shengao,String weight,String cudte){

//        cudte.replace("[{","");
//
//        String[] split = cudte.split("},\\{");
//        for (String s : split) {
//            System.out.println(s);
//        }


//        uCudetailsService.getCudetailsByName();

        return commonResult;
    }

}
